<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../../../">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>h2o_dashboard.pages.okx_streams &mdash; AntBot 0.0.1 documentation</title>
      <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css?v=80d5e7a1" />
      <link rel="stylesheet" type="text/css" href="../../../_static/css/theme.css?v=19f00094" />
      <link rel="stylesheet" type="text/css" href="../../../_static/graphviz.css?v=eafc0fe6" />

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../../../_static/jquery.js?v=5d32c60e"></script>
        <script src="../../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../../../_static/documentation_options.js?v=d45e8c67"></script>
        <script src="../../../_static/doctools.js?v=888ff710"></script>
        <script src="../../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../../../index.html" class="icon icon-home">
            AntBot
          </a>
              <div class="version">
                0.0.1
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../autodocumentation_python.html">autodocumentation_python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../firebase_tools.BaseClasses.html">firebase_tools.BaseClasses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../firebase_tools.FirestoreClient.html">firebase_tools.FirestoreClient</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../firebase_tools.authenticate.html">firebase_tools.authenticate</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.dashboard.html">h2o_dashboard.dashboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.pages.documentation_page.html">h2o_dashboard.pages.documentation_page</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.pages.okx_dashbaord_page.okx_antbot_okx_premium_indicator_handler.html">h2o_dashboard.pages.okx_dashbaord_page.okx_antbot_okx_premium_indicator_handler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.pages.okx_dashbaord_page.okx_dashboard_page.html">h2o_dashboard.pages.okx_dashbaord_page.okx_dashboard_page</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.pages.okx_streams.html">h2o_dashboard.pages.okx_streams</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.pages.overview_page.html">h2o_dashboard.pages.overview_page</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.util.html">h2o_dashboard.util</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../h2o_dashboard.wave_auth.html">h2o_dashboard.wave_auth</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.Futures_Exchange_Client.html">pyokx.Futures_Exchange_Client</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.InstrumentSearcher.html">pyokx.InstrumentSearcher</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.data_structures.html">pyokx.data_structures</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.Account.html">pyokx.low_rest_api.Account</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.BlockTrading.html">pyokx.low_rest_api.BlockTrading</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.Convert.html">pyokx.low_rest_api.Convert</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.CopyTrading.html">pyokx.low_rest_api.CopyTrading</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.Earning.html">pyokx.low_rest_api.Earning</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.FDBroker.html">pyokx.low_rest_api.FDBroker</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.Funding.html">pyokx.low_rest_api.Funding</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.Grid.html">pyokx.low_rest_api.Grid</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.MarketData.html">pyokx.low_rest_api.MarketData</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.NDBroker.html">pyokx.low_rest_api.NDBroker</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.PublicData.html">pyokx.low_rest_api.PublicData</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.SpreadTrading.html">pyokx.low_rest_api.SpreadTrading</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.Status.html">pyokx.low_rest_api.Status</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.SubAccount.html">pyokx.low_rest_api.SubAccount</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.Trade.html">pyokx.low_rest_api.Trade</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.TradingData.html">pyokx.low_rest_api.TradingData</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.client.html">pyokx.low_rest_api.client</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.consts.html">pyokx.low_rest_api.consts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.exceptions.html">pyokx.low_rest_api.exceptions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.low_rest_api.utils.html">pyokx.low_rest_api.utils</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.market_data_service.WssMarketDataService.html">pyokx.okx_market_maker.market_data_service.WssMarketDataService</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.market_data_service.model.Instrument.html">pyokx.okx_market_maker.market_data_service.model.Instrument</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.market_data_service.model.MarkPx.html">pyokx.okx_market_maker.market_data_service.model.MarkPx</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.market_data_service.model.OrderBook.html">pyokx.okx_market_maker.market_data_service.model.OrderBook</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.market_data_service.model.Tickers.html">pyokx.okx_market_maker.market_data_service.model.Tickers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.order_management_service.WssOrderManagementService.html">pyokx.okx_market_maker.order_management_service.WssOrderManagementService</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.order_management_service.model.Order.html">pyokx.okx_market_maker.order_management_service.model.Order</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.order_management_service.model.OrderRequest.html">pyokx.okx_market_maker.order_management_service.model.OrderRequest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.position_management_service.WssPositionManagementService.html">pyokx.okx_market_maker.position_management_service.WssPositionManagementService</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.position_management_service.model.Account.html">pyokx.okx_market_maker.position_management_service.model.Account</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.position_management_service.model.BalanceAndPosition.html">pyokx.okx_market_maker.position_management_service.model.BalanceAndPosition</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.position_management_service.model.Positions.html">pyokx.okx_market_maker.position_management_service.model.Positions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.run_sample_market_maker.html">pyokx.okx_market_maker.run_sample_market_maker</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.settings.html">pyokx.okx_market_maker.settings</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.strategy.BaseStrategy.html">pyokx.okx_market_maker.strategy.BaseStrategy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.strategy.SampleMM.html">pyokx.okx_market_maker.strategy.SampleMM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.strategy.model.StrategyMeasurement.html">pyokx.okx_market_maker.strategy.model.StrategyMeasurement</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.strategy.model.StrategyOrder.html">pyokx.okx_market_maker.strategy.model.StrategyOrder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.strategy.params.ParamsLoader.html">pyokx.okx_market_maker.strategy.params.ParamsLoader</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.strategy.risk.RiskCalculator.html">pyokx.okx_market_maker.strategy.risk.RiskCalculator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.strategy.risk.RiskSnapshot.html">pyokx.okx_market_maker.strategy.risk.RiskSnapshot</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.tests.test_strategy.html">pyokx.okx_market_maker.tests.test_strategy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.utils.InstrumentUtil.html">pyokx.okx_market_maker.utils.InstrumentUtil</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.utils.OkxEnum.html">pyokx.okx_market_maker.utils.OkxEnum</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.utils.TdModeUtil.html">pyokx.okx_market_maker.utils.TdModeUtil</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.okx_market_maker.utils.WsOrderUtil.html">pyokx.okx_market_maker.utils.WsOrderUtil</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.redis_structured_reports.html">pyokx.redis_structured_reports</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.redis_structured_streams.html">pyokx.redis_structured_streams</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.rest_handling.html">pyokx.rest_handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.rest_messages_service.html">pyokx.rest_messages_service</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.strategy_handling.html">pyokx.strategy_handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.AccountTest.html">pyokx.test.AccountTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.BlockTradingTest.html">pyokx.test.BlockTradingTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.BrokerTest.html">pyokx.test.BrokerTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.ConvertTest.html">pyokx.test.ConvertTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.CopyTradingTest.html">pyokx.test.CopyTradingTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.EarningTest.html">pyokx.test.EarningTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.FundingTest.html">pyokx.test.FundingTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.GridTest.html">pyokx.test.GridTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.MarketTest.html">pyokx.test.MarketTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.PublicDataTest.html">pyokx.test.PublicDataTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.SpreadTest.html">pyokx.test.SpreadTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.StackingTest.html">pyokx.test.StackingTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.SubAccountTest.html">pyokx.test.SubAccountTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.TradeTest.html">pyokx.test.TradeTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.TradingDataTest.html">pyokx.test.TradingDataTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.WsPrivateAsyncTest.html">pyokx.test.WsPrivateAsyncTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.test.WsPublicAsyncTest.html">pyokx.test.WsPublicAsyncTest</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.websocket_handling.html">pyokx.websocket_handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.ws_clients.WebSocketFactory.html">pyokx.ws_clients.WebSocketFactory</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.ws_clients.WsPprivateAsync.html">pyokx.ws_clients.WsPprivateAsync</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.ws_clients.WsPublicAsync.html">pyokx.ws_clients.WsPublicAsync</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.ws_clients.WsUtils.html">pyokx.ws_clients.WsUtils</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../pyokx.ws_data_structures.html">pyokx.ws_data_structures</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../redis_tools.config.html">redis_tools.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../redis_tools.consumers.html">redis_tools.consumers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../redis_tools.utils.html">redis_tools.utils</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../rest_app.html">rest_app</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../routers.api_keys.html">routers.api_keys</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../routers.login.html">routers.login</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../routers.okx.html">routers.okx</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../routers.okx_authentication.html">routers.okx_authentication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../routers.signup.html">routers.signup</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.command_execution.html">shared.command_execution</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.config.html">shared.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.configuration_parser.html">shared.configuration_parser</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.data_processing_utils.html">shared.data_processing_utils</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.file_directory_ops.html">shared.file_directory_ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.logging.html">shared.logging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.string_dict_utils.html">shared.string_dict_utils</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../shared.tmp_shared.html">shared.tmp_shared</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tradingview_tools.tradingview_widgets.html">tradingview_tools.tradingview_widgets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../websocket_app.html">websocket_app</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">AntBot</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../../index.html">Module code</a></li>
      <li class="breadcrumb-item active">h2o_dashboard.pages.okx_streams</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for h2o_dashboard.pages.okx_streams</h1><div class="highlight"><pre>
<span></span>
<span class="kn">import</span> <span class="nn">pprint</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>

<span class="kn">from</span> <span class="nn">h2o_wave</span> <span class="kn">import</span> <span class="n">Q</span><span class="p">,</span> <span class="n">ui</span><span class="p">,</span> <span class="n">on</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">run_on</span><span class="p">,</span> <span class="n">AsyncSite</span><span class="p">,</span> <span class="n">pack</span>  <span class="c1"># noqa F401</span>

<span class="kn">from</span> <span class="nn">h2o_dashboard.util</span> <span class="kn">import</span> <span class="n">add_card</span>
<span class="kn">from</span> <span class="nn">pyokx.data_structures</span> <span class="kn">import</span> <span class="n">AccountBalanceData</span><span class="p">,</span> <span class="n">FillHistoricalMetrics</span><span class="p">,</span> <span class="n">FillHistoricalMetricsEntry</span>
<span class="kn">from</span> <span class="nn">pyokx.redis_structured_streams</span> <span class="kn">import</span> <span class="n">get_stream_okx_account_messages</span><span class="p">,</span> <span class="n">get_stream_okx_position_messages</span><span class="p">,</span> \
    <span class="n">get_stream_okx_fill_metrics_report</span>
<span class="kn">from</span> <span class="nn">pyokx.ws_data_structures</span> <span class="kn">import</span> <span class="n">AccountChannel</span><span class="p">,</span> <span class="n">PositionsChannel</span><span class="p">,</span> <span class="n">WSPosition</span>


<div class="viewcode-block" id="OKX_Account_StreamWidget">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Account_StreamWidget">[docs]</a>
<span class="k">class</span> <span class="nc">OKX_Account_StreamWidget</span><span class="p">:</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">q</span><span class="p">:</span> <span class="n">Q</span><span class="p">,</span> <span class="n">card_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">q</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">AccountChannel</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">=</span> <span class="n">card_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">box</span> <span class="o">=</span> <span class="n">box</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;$blue&#39;</span><span class="p">,</span> <span class="s1">&#39;$red&#39;</span><span class="p">,</span> <span class="s1">&#39;$green&#39;</span><span class="p">,</span> <span class="s1">&#39;$yellow&#39;</span><span class="p">,</span> <span class="s1">&#39;$orange&#39;</span><span class="p">,</span> <span class="s1">&#39;$pink&#39;</span><span class="p">,</span> <span class="s1">&#39;$purple&#39;</span><span class="p">,</span> <span class="s1">&#39;$cyan&#39;</span><span class="p">,</span> <span class="s1">&#39;$gray&#39;</span><span class="p">]</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_update_stream</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">AccountChannel</span><span class="p">]</span> <span class="o">=</span> <span class="k">await</span> <span class="n">get_stream_okx_account_messages</span><span class="p">(</span>
            <span class="n">async_redis</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">async_redis</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_is_initialized</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">)</span>

<div class="viewcode-block" id="OKX_Account_StreamWidget.get_ui_total_equity_box_as_small_stat">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Account_StreamWidget.get_ui_total_equity_box_as_small_stat">[docs]</a>
    <span class="k">def</span> <span class="nf">get_ui_total_equity_box_as_small_stat</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">last_account_stream_entry</span><span class="p">:</span> <span class="n">AccountChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">last_account_stream_data</span> <span class="o">=</span> <span class="n">last_account_stream_entry</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="p">[</span><span class="n">account</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">totalEq</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">account</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">]</span><span class="w"> </span><span class="si">= }</span><span class="s1">&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">ui</span><span class="o">.</span><span class="n">small_series_stat_card</span><span class="p">(</span>
            <span class="n">box</span><span class="o">=</span><span class="n">box</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="s1">&#39;Total Equity&#39;</span><span class="p">,</span>
            <span class="n">value</span><span class="o">=</span><span class="s1">&#39;=${{intl total_eq minimum_fraction_digits=2 maximum_fraction_digits=2}}&#39;</span><span class="p">,</span>
            <span class="n">data</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span>
                <span class="n">u_time</span><span class="o">=</span><span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">uTime</span><span class="p">,</span>
                <span class="n">total_eq</span><span class="o">=</span><span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">totalEq</span><span class="p">),</span>
            <span class="n">plot_type</span><span class="o">=</span><span class="s1">&#39;area&#39;</span><span class="p">,</span>
            <span class="n">plot_value</span><span class="o">=</span><span class="s1">&#39;total_eq&#39;</span><span class="p">,</span>
            <span class="n">plot_color</span><span class="o">=</span><span class="s1">&#39;$green&#39;</span><span class="p">,</span>
            <span class="n">plot_data</span><span class="o">=</span><span class="n">data</span><span class="p">(</span><span class="s1">&#39;u_time total_eq&#39;</span><span class="p">,</span> <span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">,</span>
                           <span class="n">rows</span><span class="o">=</span><span class="p">[[</span><span class="n">account</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">uTime</span><span class="p">,</span> <span class="n">account</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">totalEq</span><span class="p">]</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">]),</span>
            <span class="n">plot_zero_value</span><span class="o">=</span><span class="nb">min</span><span class="p">([</span><span class="nb">float</span><span class="p">(</span><span class="n">account</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">totalEq</span><span class="p">)</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">])</span> <span class="o">*</span> <span class="mf">0.9999</span><span class="p">,</span>
            <span class="n">plot_curve</span><span class="o">=</span><span class="s1">&#39;linear&#39;</span><span class="p">,</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="OKX_Account_StreamWidget.get_ui_account_breakdown_box_as_pie_stat">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Account_StreamWidget.get_ui_account_breakdown_box_as_pie_stat">[docs]</a>
    <span class="k">def</span> <span class="nf">get_ui_account_breakdown_box_as_pie_stat</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">last_account_stream_entry</span><span class="p">:</span> <span class="n">AccountChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">last_account_stream_data</span> <span class="o">=</span> <span class="n">last_account_stream_entry</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">pies</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">colors_copy</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">for</span> <span class="n">account_balance_detail</span> <span class="ow">in</span> <span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">details</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">colors_copy</span><span class="p">:</span>
                <span class="n">colors_copy</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">colors</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">color</span> <span class="o">=</span> <span class="n">colors_copy</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="n">fraction</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">totalEq</span><span class="p">)</span>
            <span class="n">percentage_string</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">fraction</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span>
            <span class="n">pies</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">pie</span><span class="p">(</span>
                <span class="c1"># label: str,</span>
                <span class="c1"># value: str,</span>
                <span class="c1"># fraction: float,</span>
                <span class="c1"># color: str,</span>
                <span class="c1"># aux_value: str | None = None</span>
                <span class="n">label</span><span class="o">=</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">ccy</span><span class="p">,</span>
                <span class="n">value</span><span class="o">=</span><span class="n">percentage_string</span><span class="p">,</span>
                <span class="n">fraction</span><span class="o">=</span><span class="n">fraction</span><span class="p">,</span>
                <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span>
                <span class="n">aux_value</span><span class="o">=</span><span class="sa">f</span><span class="s1">&#39;$</span><span class="si">{</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span><span class="si">}</span><span class="s1"> (</span><span class="si">{</span><span class="n">percentage_string</span><span class="si">}</span><span class="s1">)&#39;</span>
            <span class="p">))</span>
        <span class="k">return</span> <span class="n">ui</span><span class="o">.</span><span class="n">wide_pie_stat_card</span><span class="p">(</span>
            <span class="c1"># box: str,</span>
            <span class="c1"># title: str,</span>
            <span class="c1"># pies: list[Pie],</span>
            <span class="c1"># commands: list[Command] | None = Non</span>
            <span class="n">box</span><span class="o">=</span><span class="n">box</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="s1">&#39;Account Breakdown&#39;</span><span class="p">,</span>
            <span class="n">pies</span><span class="o">=</span><span class="n">pies</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="OKX_Account_StreamWidget.update_account_breakdown_card">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Account_StreamWidget.update_account_breakdown_card">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_account_breakdown_card</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">last_account_stream_entry</span><span class="p">:</span> <span class="n">AccountChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">last_account_stream_data</span> <span class="o">=</span> <span class="n">last_account_stream_entry</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">account_breakdown_card</span><span class="p">:</span> <span class="n">ui</span><span class="o">.</span><span class="n">wide_pie_stat_card</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_account_breakdown&#39;</span><span class="p">]</span>
        <span class="n">details</span> <span class="o">=</span> <span class="p">{</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">ccy</span><span class="p">:</span> <span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span> <span class="k">for</span> <span class="n">account_balance_detail</span> <span class="ow">in</span>
                   <span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">details</span><span class="p">}</span>
        <span class="k">for</span> <span class="n">pie</span><span class="p">,</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">account_breakdown_card</span><span class="o">.</span><span class="n">pies</span><span class="p">,</span> <span class="n">details</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
            <span class="n">fraction</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">totalEq</span><span class="p">)</span>
            <span class="n">percentage_string</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">fraction</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span>
            <span class="n">pie</span><span class="o">.</span><span class="n">label</span> <span class="o">=</span> <span class="n">k</span>
            <span class="n">pie</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">percentage_string</span>
            <span class="n">pie</span><span class="o">.</span><span class="n">fraction</span> <span class="o">=</span> <span class="n">fraction</span>
            <span class="n">pie</span><span class="o">.</span><span class="n">aux_value</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;$</span><span class="si">{</span><span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">),</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="si">}</span><span class="s1"> (</span><span class="si">{</span><span class="n">percentage_string</span><span class="si">}</span><span class="s1">)&#39;</span></div>


<div class="viewcode-block" id="OKX_Account_StreamWidget.update_total_equity_card">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Account_StreamWidget.update_total_equity_card">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_total_equity_card</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">last_account_stream_entry</span><span class="p">:</span> <span class="n">AccountChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">last_account_stream_data</span> <span class="o">=</span> <span class="n">last_account_stream_entry</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">total_equity_card</span><span class="p">:</span> <span class="n">ui</span><span class="o">.</span><span class="n">small_series_stat_card</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_total_equity&#39;</span><span class="p">]</span>
        <span class="n">total_equity_card</span><span class="o">.</span><span class="n">plot_data</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">uTime</span><span class="p">,</span> <span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">totalEq</span><span class="p">]</span>
        <span class="n">total_equity_card</span><span class="o">.</span><span class="n">plot_zero_value</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span>
            <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="n">account</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">totalEq</span><span class="p">)</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">account_stream</span><span class="p">])</span> <span class="o">*</span> <span class="mf">0.9999</span>
        <span class="n">total_equity_card</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="n">u_time</span><span class="o">=</span><span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">uTime</span><span class="p">,</span>
            <span class="n">total_eq</span><span class="o">=</span><span class="n">last_account_stream_data</span><span class="o">.</span><span class="n">totalEq</span><span class="p">)</span></div>


<div class="viewcode-block" id="OKX_Account_StreamWidget.add_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Account_StreamWidget.add_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">add_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="k">await</span> <span class="n">add_card</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_total_equity&#39;</span><span class="p">,</span>
                       <span class="bp">self</span><span class="o">.</span><span class="n">get_ui_total_equity_box_as_small_stat</span><span class="p">(</span><span class="n">box</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="p">))</span>
        <span class="k">await</span> <span class="n">add_card</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_account_breakdown&#39;</span><span class="p">,</span>
                       <span class="bp">self</span><span class="o">.</span><span class="n">get_ui_account_breakdown_box_as_pie_stat</span><span class="p">(</span><span class="n">box</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="p">))</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>


<div class="viewcode-block" id="OKX_Account_StreamWidget.update_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Account_StreamWidget.update_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_total_equity_card</span><span class="p">()</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_account_breakdown_card</span><span class="p">()</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>
</div>



<div class="viewcode-block" id="OKX_Positions_StreamWidget">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Positions_StreamWidget">[docs]</a>
<span class="k">class</span> <span class="nc">OKX_Positions_StreamWidget</span><span class="p">:</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">q</span><span class="p">:</span> <span class="n">Q</span><span class="p">,</span> <span class="n">card_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">q</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">positions_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">PositionsChannel</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">=</span> <span class="n">card_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">box</span> <span class="o">=</span> <span class="n">box</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_column_mappings</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="n">posId</span><span class="o">=</span><span class="s1">&#39;Position ID&#39;</span><span class="p">,</span>
            <span class="n">instId</span><span class="o">=</span><span class="s1">&#39;Instrument ID&#39;</span><span class="p">,</span>
            <span class="n">pos</span><span class="o">=</span><span class="s1">&#39;Position&#39;</span><span class="p">,</span>
            <span class="n">upl</span><span class="o">=</span><span class="s1">&#39;UPL&#39;</span><span class="p">,</span>
            <span class="n">avgPx</span><span class="o">=</span><span class="s1">&#39;Average Price&#39;</span><span class="p">,</span>
            <span class="n">lever</span><span class="o">=</span><span class="s1">&#39;Leverage&#39;</span><span class="p">,</span>
            <span class="n">last</span><span class="o">=</span><span class="s1">&#39;Last&#39;</span><span class="p">,</span>
            <span class="n">margin</span><span class="o">=</span><span class="s1">&#39;Margin&#39;</span><span class="p">,</span>
            <span class="n">pTime</span><span class="o">=</span><span class="s1">&#39;Time&#39;</span>
        <span class="p">)</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_update_stream</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">positions_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">PositionsChannel</span><span class="p">]</span> <span class="o">=</span> <span class="k">await</span> <span class="n">get_stream_okx_position_messages</span><span class="p">(</span>
            <span class="n">async_redis</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">async_redis</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">positions_stream</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_is_initialized</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">positions_stream</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>

<div class="viewcode-block" id="OKX_Positions_StreamWidget.get_ui_live_positions_table">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Positions_StreamWidget.get_ui_live_positions_table">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">get_ui_live_positions_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">if</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_initialized</span><span class="p">():</span>
            <span class="n">latest_positions_report</span><span class="p">:</span> <span class="n">PositionsChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">positions_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">latest_positions_data</span> <span class="o">=</span> <span class="n">latest_positions_report</span><span class="o">.</span><span class="n">data</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">latest_positions_data</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">position</span> <span class="ow">in</span> <span class="n">latest_positions_data</span><span class="p">:</span>
            <span class="n">position</span><span class="p">:</span> <span class="n">WSPosition</span> <span class="o">=</span> <span class="n">position</span>
            <span class="n">columns_values</span> <span class="o">=</span> <span class="p">[</span><span class="nb">getattr</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">col</span><span class="p">)</span> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_column_mappings</span><span class="o">.</span><span class="n">keys</span><span class="p">()]</span>
            <span class="n">label</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">columns_values</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">pos_col</span> <span class="ow">in</span> <span class="n">columns_values</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">pos_col</span><span class="p">),</span> <span class="mi">2</span><span class="p">))</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                <span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="n">values</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span><span class="p">(</span>
            <span class="n">box</span><span class="o">=</span><span class="n">box</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="s1">&#39;Live Positions - okx:websockets@positions&#39;</span><span class="p">,</span>
            <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_column_mappings</span><span class="o">.</span><span class="n">values</span><span class="p">()),</span>
            <span class="n">items</span><span class="o">=</span><span class="n">items</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="OKX_Positions_StreamWidget.add_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Positions_StreamWidget.add_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">add_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="k">await</span> <span class="n">add_card</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_positions_table&#39;</span><span class="p">,</span>
                       <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_ui_live_positions_table</span><span class="p">(</span><span class="n">box</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="p">))</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>


<div class="viewcode-block" id="OKX_Positions_StreamWidget.update_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Positions_StreamWidget.update_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="k">if</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_initialized</span><span class="p">():</span>
            <span class="n">latest_positions_report</span><span class="p">:</span> <span class="n">PositionsChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">positions_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="n">latest_positions_data</span> <span class="o">=</span> <span class="n">latest_positions_report</span><span class="o">.</span><span class="n">data</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">latest_positions_data</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">positions_table_card</span><span class="p">:</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_positions_table&#39;</span><span class="p">]</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">position</span> <span class="ow">in</span> <span class="n">latest_positions_data</span><span class="p">:</span>
            <span class="n">position</span><span class="p">:</span> <span class="n">WSPosition</span> <span class="o">=</span> <span class="n">position</span>
            <span class="n">columns_values</span> <span class="o">=</span> <span class="p">[</span><span class="nb">getattr</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">col</span><span class="p">)</span> <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_column_mappings</span><span class="o">.</span><span class="n">keys</span><span class="p">()]</span>
            <span class="n">label</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">columns_values</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">pos_col</span> <span class="ow">in</span> <span class="n">columns_values</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">pos_col</span><span class="p">),</span> <span class="mi">2</span><span class="p">))</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                <span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="n">values</span><span class="p">))</span>
        <span class="n">positions_table_card</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="n">items</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>
</div>



<div class="viewcode-block" id="OKX_Fill_Report_StreamWidget">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Fill_Report_StreamWidget">[docs]</a>
<span class="k">class</span> <span class="nc">OKX_Fill_Report_StreamWidget</span><span class="p">:</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">q</span><span class="p">:</span> <span class="n">Q</span><span class="p">,</span> <span class="n">card_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">q</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fill_metrics_report_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FillHistoricalMetrics</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">=</span> <span class="n">card_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">box</span> <span class="o">=</span> <span class="n">box</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_update_stream</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fill_metrics_report_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">FillHistoricalMetrics</span><span class="p">]</span> <span class="o">=</span> <span class="k">await</span> <span class="n">get_stream_okx_fill_metrics_report</span><span class="p">(</span>
            <span class="n">async_redis</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">async_redis</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">fill_metrics_report_stream</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_is_initialized</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fill_metrics_report_stream</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>

<div class="viewcode-block" id="OKX_Fill_Report_StreamWidget.get_ui_fill_metrics_report">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Fill_Report_StreamWidget.get_ui_fill_metrics_report">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">get_ui_fill_metrics_report</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">if</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_initialized</span><span class="p">():</span>
            <span class="n">latest_fill_metrics_report</span><span class="p">:</span> <span class="n">FillHistoricalMetrics</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fill_metrics_report_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">latest_fill_metrics_report</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">timeframe</span><span class="p">,</span> <span class="n">fill_metrics</span> <span class="ow">in</span> <span class="n">latest_fill_metrics_report</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">timeframe</span><span class="w"> </span><span class="si">= }</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">fill_metrics</span><span class="w"> </span><span class="si">= }</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span>
                <span class="n">label</span><span class="o">=</span><span class="n">timeframe</span><span class="p">,</span>
                <span class="n">values</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">fill_metrics</span><span class="o">.</span><span class="n">avg_fill_pnl</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">fill_metrics</span><span class="o">.</span><span class="n">total_fill_pnl</span><span class="p">),</span>
                        <span class="nb">str</span><span class="p">(</span><span class="n">fill_metrics</span><span class="o">.</span><span class="n">total_fill_fee</span><span class="p">)]</span>
            <span class="p">))</span>
        <span class="k">return</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span><span class="p">(</span>
            <span class="n">box</span><span class="o">=</span><span class="n">box</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="s1">&#39;Fill Metrics Report - okx:reports@fill_metrics&#39;</span><span class="p">,</span>
            <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Timeframe&#39;</span><span class="p">,</span> <span class="s1">&#39;Avg Fill PnL&#39;</span><span class="p">,</span> <span class="s1">&#39;Total Fill PnL&#39;</span><span class="p">,</span> <span class="s1">&#39;Total Fill Fee&#39;</span><span class="p">],</span>
            <span class="n">items</span><span class="o">=</span><span class="n">items</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="OKX_Fill_Report_StreamWidget.add_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Fill_Report_StreamWidget.add_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">add_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="k">await</span> <span class="n">add_card</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_fill_metrics_report&#39;</span><span class="p">,</span>
                       <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_ui_fill_metrics_report</span><span class="p">(</span><span class="n">box</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="p">))</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>


<div class="viewcode-block" id="OKX_Fill_Report_StreamWidget.update_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.OKX_Fill_Report_StreamWidget.update_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="k">if</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_initialized</span><span class="p">():</span>
            <span class="n">latest_fill_metrics_report</span><span class="p">:</span> <span class="n">FillHistoricalMetrics</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fill_metrics_report_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">latest_fill_metrics_report</span> <span class="o">=</span> <span class="n">FillHistoricalMetrics</span><span class="p">(</span>
                <span class="n">ONE_DAY</span><span class="o">=</span><span class="n">FillHistoricalMetricsEntry</span><span class="p">(</span><span class="n">avg_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_fee</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span>
                <span class="n">ONE_WEEK</span><span class="o">=</span><span class="n">FillHistoricalMetricsEntry</span><span class="p">(</span><span class="n">avg_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_fee</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span>
                <span class="n">ONE_MONTH</span><span class="o">=</span><span class="n">FillHistoricalMetricsEntry</span><span class="p">(</span><span class="n">avg_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_fee</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span>
                <span class="n">THREE_MONTHS</span><span class="o">=</span><span class="n">FillHistoricalMetricsEntry</span><span class="p">(</span><span class="n">avg_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_pnl</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_fill_fee</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">fill_metrics_report_card</span><span class="p">:</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_fill_metrics_report&#39;</span><span class="p">]</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">timeframe</span><span class="p">,</span> <span class="n">fill_metrics</span> <span class="ow">in</span> <span class="n">latest_fill_metrics_report</span><span class="p">:</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span>
                <span class="n">label</span><span class="o">=</span><span class="n">timeframe</span><span class="p">,</span>
                <span class="n">values</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">fill_metrics</span><span class="o">.</span><span class="n">avg_fill_pnl</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">fill_metrics</span><span class="o">.</span><span class="n">total_fill_pnl</span><span class="p">),</span>
                        <span class="nb">str</span><span class="p">(</span><span class="n">fill_metrics</span><span class="o">.</span><span class="n">total_fill_fee</span><span class="p">)]</span>
            <span class="p">))</span>
        <span class="n">fill_metrics_report_card</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="n">items</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>
</div>




<div class="viewcode-block" id="Overview_StreamWidget">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget">[docs]</a>
<span class="k">class</span> <span class="nc">Overview_StreamWidget</span><span class="p">:</span>  <span class="c1"># TODO: Will connect to the account streams for all exchanges, rn it&#39;s just okx</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">q</span><span class="p">:</span> <span class="n">Q</span><span class="p">,</span> <span class="n">card_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">10</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">q</span> <span class="o">=</span> <span class="n">q</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">=</span> <span class="n">card_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;$blue&#39;</span><span class="p">,</span> <span class="s1">&#39;$red&#39;</span><span class="p">,</span> <span class="s1">&#39;$green&#39;</span><span class="p">,</span> <span class="s1">&#39;$yellow&#39;</span><span class="p">,</span> <span class="s1">&#39;$orange&#39;</span><span class="p">,</span> <span class="s1">&#39;$pink&#39;</span><span class="p">,</span> <span class="s1">&#39;$purple&#39;</span><span class="p">,</span> <span class="s1">&#39;$cyan&#39;</span><span class="p">,</span> <span class="s1">&#39;$gray&#39;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">okx_account_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">AccountChannel</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">okx_positions_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">PositionsChannel</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">okx_position_column_mappings</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="n">instId</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Instrument ID&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;str&#39;</span><span class="p">},</span>
            <span class="n">posId</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Position ID&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;int&#39;</span><span class="p">},</span>
            <span class="n">pos</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Position&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;float&#39;</span><span class="p">},</span>
            <span class="n">upl</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;UPnL&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;float&#39;</span><span class="p">},</span>
            <span class="n">avgPx</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Avg Price&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;float&#39;</span><span class="p">},</span>
            <span class="n">lever</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Lev&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;float&#39;</span><span class="p">},</span>
            <span class="n">last</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Last&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;float&#39;</span><span class="p">},</span>
            <span class="n">margin</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Margin&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;float&#39;</span><span class="p">},</span>
            <span class="n">pTime</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;label&#39;</span><span class="p">:</span> <span class="s1">&#39;Time&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="s1">&#39;timestamp&#39;</span><span class="p">},</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">box</span> <span class="o">=</span> <span class="n">box</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_update_stream</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">okx_account_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">AccountChannel</span><span class="p">]</span> <span class="o">=</span> <span class="k">await</span> <span class="n">get_stream_okx_account_messages</span><span class="p">(</span>
            <span class="n">async_redis</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">async_redis</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">okx_positions_stream</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">PositionsChannel</span><span class="p">]</span> <span class="o">=</span> <span class="k">await</span> <span class="n">get_stream_okx_position_messages</span><span class="p">(</span>
            <span class="n">async_redis</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">async_redis</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_is_initialized</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">okx_account_stream</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">okx_positions_stream</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>

<div class="viewcode-block" id="Overview_StreamWidget.get_all_exchanges_account_breakdown_table_card">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget.get_all_exchanges_account_breakdown_table_card">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">get_all_exchanges_account_breakdown_table_card</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="c1"># Table Exchange:Asset|Total-Asset-Value|FreeAssetCash</span>
        <span class="n">latest_account_report</span><span class="p">:</span> <span class="n">AccountChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">okx_account_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">latest_account_data</span><span class="p">:</span> <span class="n">AccountBalanceData</span> <span class="o">=</span> <span class="n">latest_account_report</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c1"># TODO Use values from all exchanges, then generate the ui item for each exchange prior to ui.stat ...</span>
        <span class="n">okx_total_equity</span> <span class="o">=</span> <span class="n">latest_account_data</span><span class="o">.</span><span class="n">totalEq</span>
        <span class="n">bitget_total_equity</span> <span class="o">=</span> <span class="mi">0</span>  <span class="c1"># TODO</span>
        <span class="n">total_equity</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">okx_total_equity</span><span class="p">)</span> <span class="o">+</span> <span class="nb">float</span><span class="p">(</span><span class="n">bitget_total_equity</span><span class="p">)</span>

<span class="w">        </span><span class="sd">&#39;&#39;&#39;OKX Exchange&#39;&#39;&#39;</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">account_balance_detail</span> <span class="ow">in</span> <span class="n">latest_account_data</span><span class="o">.</span><span class="n">details</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;OKX:</span><span class="si">{</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">ccy</span><span class="si">}</span><span class="s1">&#39;</span>
            <span class="n">total_asset_value</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">unused_value</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">availBal</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span>
                    <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">disEq</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eq</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">fraction_of_total_equity</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">total_equity</span><span class="p">)</span>
            <span class="n">percentage_string</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">fraction_of_total_equity</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span>
            <span class="n">update_time</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">uTime</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span>
                <span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
            <span class="c1">#</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">total_asset_value</span><span class="p">,</span> <span class="n">unused_value</span><span class="p">,</span> <span class="n">percentage_string</span><span class="p">,</span> <span class="n">update_time</span><span class="p">]</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">values</span><span class="p">]))</span>

        <span class="k">return</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span><span class="p">(</span>
            <span class="n">box</span><span class="o">=</span><span class="n">box</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="sa">f</span><span class="s1">&#39;Live Account - </span><span class="se">{{</span><span class="s1">exchange</span><span class="se">}}</span><span class="s1">:websockets@account - $</span><span class="si">{</span><span class="nb">round</span><span class="p">(</span><span class="n">total_equity</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span>
            <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;Exchange:Asset&#39;</span><span class="p">,</span> <span class="s1">&#39;Total-Asset-Value&#39;</span><span class="p">,</span> <span class="s1">&#39;FreeAssetCash&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">% o</span><span class="s1">f Total Equity&#39;</span><span class="p">,</span> <span class="s1">&#39;Update Time&#39;</span><span class="p">],</span>
            <span class="n">items</span><span class="o">=</span><span class="n">items</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="Overview_StreamWidget.get_all_exchanges_live_positions_table_card">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget.get_all_exchanges_live_positions_table_card">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">get_all_exchanges_live_positions_table_card</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">latest_positions_report</span><span class="p">:</span> <span class="n">PositionsChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">okx_positions_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">latest_positions_data</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">WSPosition</span><span class="p">]</span> <span class="o">=</span> <span class="n">latest_positions_report</span><span class="o">.</span><span class="n">data</span>

        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">label_value</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">for</span> <span class="n">position</span> <span class="ow">in</span> <span class="n">latest_positions_data</span><span class="p">:</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">col_key</span><span class="p">,</span> <span class="n">col_value</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">okx_position_column_mappings</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">label_value</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;OKX:</span><span class="si">{</span><span class="nb">getattr</span><span class="p">(</span><span class="n">position</span><span class="p">,</span><span class="w"> </span><span class="n">col_key</span><span class="p">)</span><span class="si">}</span><span class="s1">&#39;</span>
                    <span class="k">continue</span>
                <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">col_key</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">expected_type</span> <span class="o">=</span> <span class="n">col_value</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">expected_type</span> <span class="o">==</span> <span class="s1">&#39;float&#39;</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">pos_col</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">expected_type</span> <span class="o">==</span> <span class="s1">&#39;int&#39;</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">expected_type</span> <span class="o">==</span> <span class="s1">&#39;timestamp&#39;</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span>
                            <span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                <span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">label_value</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="n">values</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span><span class="p">(</span>
            <span class="n">box</span><span class="o">=</span><span class="n">box</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="sa">f</span><span class="s1">&#39;Live Positions - </span><span class="se">{{</span><span class="s1">exchange</span><span class="se">}}</span><span class="s1">:websockets@positions - </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="n">latest_positions_data</span><span class="p">)</span><span class="si">}</span><span class="s1"> positions&#39;</span><span class="p">,</span>
            <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="n">col_value</span><span class="p">[</span><span class="s1">&#39;label&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">col_value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">okx_position_column_mappings</span><span class="o">.</span><span class="n">values</span><span class="p">()],</span>
            <span class="n">items</span><span class="o">=</span><span class="n">items</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="Overview_StreamWidget.get_all_exchanges_account_breakdown_pie_chart_card">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget.get_all_exchanges_account_breakdown_pie_chart_card">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">get_all_exchanges_account_breakdown_pie_chart_card</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">box</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">latest_account_report</span><span class="p">:</span> <span class="n">AccountChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">okx_account_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">latest_account_data</span><span class="p">:</span> <span class="n">AccountBalanceData</span> <span class="o">=</span> <span class="n">latest_account_report</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="c1"># Pie Chart</span>
        <span class="n">pies</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">colors_copy</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">COLORS</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;$blue&#39;</span><span class="p">,</span> <span class="s1">&#39;$red&#39;</span><span class="p">,</span> <span class="s1">&#39;$green&#39;</span><span class="p">,</span> <span class="s1">&#39;$yellow&#39;</span><span class="p">,</span> <span class="s1">&#39;$orange&#39;</span><span class="p">,</span> <span class="s1">&#39;$pink&#39;</span><span class="p">,</span> <span class="s1">&#39;$purple&#39;</span><span class="p">,</span> <span class="s1">&#39;$cyan&#39;</span><span class="p">,</span> <span class="s1">&#39;$gray&#39;</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">account_balance_detail</span> <span class="ow">in</span> <span class="n">latest_account_data</span><span class="o">.</span><span class="n">details</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">colors_copy</span><span class="p">:</span>
                <span class="n">colors_copy</span> <span class="o">=</span> <span class="n">COLORS</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">color</span> <span class="o">=</span> <span class="n">colors_copy</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="n">fraction</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">latest_account_data</span><span class="o">.</span><span class="n">totalEq</span><span class="p">)</span>
            <span class="n">percentage_string</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">fraction</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span>
            <span class="n">pies</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">pie</span><span class="p">(</span>
                <span class="c1"># label: str,</span>
                <span class="c1"># value: str,</span>
                <span class="c1"># fraction: float,</span>
                <span class="c1"># color: str,</span>
                <span class="c1"># aux_value: str | None = None</span>
                <span class="n">label</span><span class="o">=</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">ccy</span><span class="p">,</span>
                <span class="n">value</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span>
                <span class="n">fraction</span><span class="o">=</span><span class="n">fraction</span><span class="p">,</span>
                <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span>
                <span class="n">aux_value</span><span class="o">=</span><span class="sa">f</span><span class="s1">&#39;(</span><span class="si">{</span><span class="n">percentage_string</span><span class="si">}</span><span class="s1">)&#39;</span>
            <span class="p">))</span>

        <span class="k">return</span> <span class="n">ui</span><span class="o">.</span><span class="n">wide_pie_stat_card</span><span class="p">(</span>
            <span class="c1"># box: str,</span>
            <span class="c1"># title: str,</span>
            <span class="c1"># pies: list[Pie],</span>
            <span class="c1"># commands: list[Command] | None = Non</span>
            <span class="n">box</span><span class="o">=</span><span class="n">box</span><span class="p">,</span>
            <span class="n">title</span><span class="o">=</span><span class="s1">&#39;All Exchanges Account Breakdown&#39;</span><span class="p">,</span>
            <span class="n">pies</span><span class="o">=</span><span class="n">pies</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="Overview_StreamWidget.update_all_exchanges_account_breakdown_table_card">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget.update_all_exchanges_account_breakdown_table_card">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_all_exchanges_account_breakdown_table_card</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">latest_account_report</span><span class="p">:</span> <span class="n">AccountChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">okx_account_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">latest_account_data</span><span class="p">:</span> <span class="n">AccountBalanceData</span> <span class="o">=</span> <span class="n">latest_account_report</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c1"># TODO Use values from all exchanges, then generate the ui item for each exchange prior to ui.stat ...</span>
        <span class="n">okx_total_equity</span> <span class="o">=</span> <span class="n">latest_account_data</span><span class="o">.</span><span class="n">totalEq</span>
        <span class="n">bitget_total_equity</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">total_equity</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">okx_total_equity</span><span class="p">)</span> <span class="o">+</span> <span class="nb">float</span><span class="p">(</span><span class="n">bitget_total_equity</span><span class="p">)</span>
        <span class="n">account_breakdown_card</span><span class="p">:</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_overview_accounts_table&#39;</span><span class="p">]</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">account_balance_detail</span> <span class="ow">in</span> <span class="n">latest_account_data</span><span class="o">.</span><span class="n">details</span><span class="p">:</span>
            <span class="n">label</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;OKX:</span><span class="si">{</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">ccy</span><span class="si">}</span><span class="s1">&#39;</span>
            <span class="n">total_asset_value</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">unused_value</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">availBal</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span>
                    <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">disEq</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eq</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">fraction_of_total_equity</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">eqUsd</span><span class="p">)</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">total_equity</span><span class="p">)</span>
            <span class="n">percentage_string</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">fraction_of_total_equity</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="o">+</span> <span class="s1">&#39;%&#39;</span>
            <span class="n">update_time</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">account_balance_detail</span><span class="o">.</span><span class="n">uTime</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span>
                <span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
            <span class="c1">#</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="n">total_asset_value</span><span class="p">,</span> <span class="n">unused_value</span><span class="p">,</span> <span class="n">percentage_string</span><span class="p">,</span> <span class="n">update_time</span><span class="p">]</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">values</span><span class="p">]))</span>
        <span class="n">account_breakdown_card</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="n">items</span>
        <span class="n">account_breakdown_card</span><span class="o">.</span><span class="n">title</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;Live Account - </span><span class="se">{{</span><span class="s1">exchange</span><span class="se">}}</span><span class="s1">:websockets@account - $</span><span class="si">{</span><span class="nb">round</span><span class="p">(</span><span class="n">total_equity</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="si">}</span><span class="s1">&#39;</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>


<div class="viewcode-block" id="Overview_StreamWidget.update_all_exchanges_live_positions_table_card">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget.update_all_exchanges_live_positions_table_card">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_all_exchanges_live_positions_table_card</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">latest_positions_report</span><span class="p">:</span> <span class="n">PositionsChannel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">okx_positions_stream</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">latest_positions_data</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">WSPosition</span><span class="p">]</span> <span class="o">=</span> <span class="n">latest_positions_report</span><span class="o">.</span><span class="n">data</span>

        <span class="n">positions_table_card</span><span class="p">:</span> <span class="n">ui</span><span class="o">.</span><span class="n">stat_table_card</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_overview_positions_table&#39;</span><span class="p">]</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">position</span> <span class="ow">in</span> <span class="n">latest_positions_data</span><span class="p">:</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">col_key</span><span class="p">,</span> <span class="n">col_value</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">okx_position_column_mappings</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">label_value</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;OKX:</span><span class="si">{</span><span class="nb">getattr</span><span class="p">(</span><span class="n">position</span><span class="p">,</span><span class="w"> </span><span class="n">col_key</span><span class="p">)</span><span class="si">}</span><span class="s1">&#39;</span>
                    <span class="k">continue</span>
                <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">col_key</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">expected_type</span> <span class="o">=</span> <span class="n">col_value</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">expected_type</span> <span class="o">==</span> <span class="s1">&#39;float&#39;</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">pos_col</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">expected_type</span> <span class="o">==</span> <span class="s1">&#39;int&#39;</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">expected_type</span> <span class="o">==</span> <span class="s1">&#39;timestamp&#39;</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span>
                            <span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">pos_col</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">pos_col</span><span class="p">)</span>
                <span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ui</span><span class="o">.</span><span class="n">stat_table_item</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">label_value</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="n">values</span><span class="p">))</span>
        <span class="n">positions_table_card</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="n">items</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>


<div class="viewcode-block" id="Overview_StreamWidget.add_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget.add_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">add_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="c1"># await add_card(self.q, self.card_name + &#39;Accounts_PieChart&#39;,</span>
        <span class="c1">#                await self.get_all_exchanges_account_breakdown_pie_chart_card(box=&#39;first_context_1&#39;))</span>
        <span class="k">await</span> <span class="n">add_card</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_overview_accounts_table&#39;</span><span class="p">,</span>
                       <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_exchanges_account_breakdown_table_card</span><span class="p">(</span><span class="n">box</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="p">))</span>
        <span class="k">await</span> <span class="n">add_card</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">card_name</span> <span class="o">+</span> <span class="s1">&#39;_overview_positions_table&#39;</span><span class="p">,</span>
                       <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_all_exchanges_live_positions_table_card</span><span class="p">(</span><span class="n">box</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="p">))</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>


<div class="viewcode-block" id="Overview_StreamWidget.update_cards">
<a class="viewcode-back" href="../../../h2o_dashboard.pages.okx_streams.html#h2o_dashboard.pages.okx_streams.Overview_StreamWidget.update_cards">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">update_cards</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_stream</span><span class="p">()</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">update_all_exchanges_account_breakdown_table_card</span><span class="p">()</span>
        <span class="c1"># await self.update_all_exchanges_live_positions_table_card()</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">q</span><span class="o">.</span><span class="n">page</span><span class="o">.</span><span class="n">save</span><span class="p">()</span></div>
</div>

</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2024, Ruben Fernandez Carbon.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>